const app = getApp();

Page({
    data: {
        active: 0,
        paidCourses: [],
        unpaidCourses: [],
        user: null
    },
    onLoad: function () {
        this.fetchOrderData();
    },
    navigateBack() {
        wx.navigateBack(); // 返回上一页
    },
    onTabChange(e) {
        this.setData({
            active: e.detail.index
        });
    },
    goToPay() {
        wx.showToast({
            title: '跳转到支付页面',
            icon: 'none'
        });
        // 跳转到支付页面的逻辑
    },
    onLoadMorePaid() {
        this.fetchMoreData('paid');
    },
    onLoadMoreUnpaid() {
        this.fetchMoreData('unpaid');
    },
    fetchOrderData() {
        const userId = app.globalData.user.id; // 从全局变量中获取 userId
        wx.request({
            url: `http://localhost:8080/hql/order/user`,
            method: 'GET',
            data: {
                userId: userId
            },
            header: {
              'Content-Type': 'application/json',
              'Authorization': 'Bearer ' + app.globalData.user.token // 在请求头中
            },
            success: (res) => {
                if (res.statusCode === 200 && res.data.code === 200) {
                    const data = res.data.data || [];
                    console.log('kecheng',data);
                    const paidCourses = data.filter(item => item.isPaid);
                    const unpaidCourses = data.filter(item => !item.isPaid);
                    
                    app.globalData.paidCourses = paidCourses;
                    app.globalData.unpaidCourses = unpaidCourses;
                    
                    this.setData({
                        paidCourses: paidCourses,
                        unpaidCourses: unpaidCourses,
                        user: app.globalData.user
                    });
                } else {
                    console.error('获取数据失败:', res);
                    wx.showToast({
                        title: '获取数据失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                console.error('请求失败:', err);
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                });
            }
        });
    },
    fetchMoreData(type) {
        const userId = app.globalData.userId;
        wx.request({
            url: `${app.globalData.baseUrl}/order/user/more`,
            method: 'GET',
            data: {
                userId: userId,
                type: type
            },
            header: {
                'Content-Type': 'application/json'
            },
            success: (res) => {
                if (res.statusCode === 200 && res.data.code === 200) {
                    const newItems = res.data.data || [];
                    let globalDataKey, setDataKey, filteredItems;
                    
                    if (type === 'paid') {
                        filteredItems = newItems.filter(item => item.isPaid);
                        globalDataKey = 'paidCourses';
                        setDataKey = 'paidCourses';
                    } else {
                        filteredItems = newItems.filter(item => !item.isPaid);
                        globalDataKey = 'unpaidCourses';
                        setDataKey = 'unpaidCourses';
                    }
                    
                    app.globalData[globalDataKey] = [...app.globalData[globalDataKey], ...filteredItems];
                    this.setData({
                        [setDataKey]: app.globalData[globalDataKey]
                    });
                } else {
                    wx.showToast({
                        title: type === 'paid' ? '没有更多已支付课程' : '没有更多未支付课程',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                console.error('请求失败:', err);
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                });
            }
        });
    }
});